package 选择排序;
/*
 * i从length - 1开始向左找一个最大的放在I的位置，然后i--，循环{
 * 假定max = i的位置
 *   j是i的左边边第一个开始遍历，找到一个最大的标记为max，和i的位置交换
 * }
 *
 * */
public class NewSelectionSort {
    private NewSelectionSort(){}
    private static <E>  void swap(E[]arr, int i, int j){
        E temp = arr[j];
        arr[j] = arr[i];
        arr[i] = temp;
    }
//    继承comparable代表可比较
    public static <E extends Comparable<E>> void sort(E []arr){
//        arr[0...i)无序, arr[i,..n)有序
        for (int i = arr.length - 1; i > 0; i--) {
            int maxIndex = i;
            for (int j = i-1; j >= 0; j--) {
                if (arr[j].compareTo(arr[maxIndex]) > 0){
                   maxIndex = j;
                }
            }
            swap(arr, i, maxIndex);
        }
    }

    public static void main(String[] args) {
        Student student1 = new Student("s", 100);
        Student student2 = new Student("s", 99);
        Student student3 = new Student("s", 98);

        Student arr[] = {student1, student2, student3};
        NewSelectionSort.sort(arr);
        for (Student student :
                arr) {
            System.out.println(student);
        }
    }
}
